#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
"""
@File    : exp_read_excel.py
@Author  : different && 道阻且长,行则将至 行而不辍,未来可期
@Time    : 2021/9/14 15:10
@Desc    : excel读取并格式化数据

"""
import numpy as np
import pandas as pd
import ujson

from settings import UPLOAD_FOLDER, TITLE_MAP


# 获取excel数据
class ReadExcel:
    def __init__(self, filename):
        self.filename = filename
        self.data_source_df = pd.read_excel(UPLOAD_FOLDER / filename, sheet_name='数据源')
        self.source_df = None
        self.attr_df = None
        self.source_list = []

    # 修改上传数据为字符串
    @staticmethod
    def format_int(data: dict):
        for k in data.keys():
            if isinstance(data[k], np.integer):
                data[k] = int(data[k])
            elif isinstance(data[k], np.floating):
                data[k] = float(data[k])
        return data

    # 删除数据中的nan
    def del_nan(self):  # 时间复杂度 O(n²)
        for d in self.source_list:
            for k in list(d.keys()):  # 循环中会改变字典，所以不能使用常规字典遍历方法
                # np中的nan是不相等的特性
                if not d[k] == d[k]:
                    del d[k]

    def data_collation(self):
        if '上架天数' in self.data_source_df.columns:
            self.data_source_df.drop('上架天数', axis=1, inplace=True)
        if '图片' in self.data_source_df.columns:
            self.data_source_df.drop('图片', axis=1, inplace=True)
        self.source_df = self.data_source_df.iloc[:, :33]
        self.source_df.dropna(axis=1, how='all', inplace=True)
        self.source_df.rename(columns=TITLE_MAP, inplace=True)
        self.attr_df = self.data_source_df.iloc[:, 33:]

    # 属性格式化
    def data_source(self):
        self.data_collation()
        for i in range(len(self.data_source_df)):
            ls = dict(zip(list(self.source_df), self.source_df.loc[i]))
            if not ls['asin'] == ls['asin']:
                break
            ls['attr'] = ujson.dumps(self.format_int(dict(self.attr_df.loc[i])), ensure_ascii=False,
                                     escape_forward_slashes=False)
            self.source_list.append(ls)
        self.del_nan()
        return self.source_list
